import ProjectModel from "../../schema/project";
import { Request, Response } from "express";
import type Typings from "../../typings";

// Common 类的实例
import { Common, ResStatus } from "../common/common";
const CommonIns = new Common(ProjectModel);
const ResStatusIns = new ResStatus();

class Project {
  constructor() {}

  // 项目树
  async tree(req: Request, res: Response) {
    try {
      const projects: Typings.Project.props[] = await CommonIns.Find({
        parentCode: "",
      });

      for (let i = 0, len = projects.length; i < len; i++) {
        projects[i].children = await CommonIns.Find({
          parentCode: projects[i].code,
        });
      }

      ResStatusIns.Success(res, projects);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 新增
  async add(req: Request, res: Response) {
    try {
      const creatorId = CommonIns.getLoginUserId(
        req as Typings.Express.ConvertRequest
      );

      const { parentCode, code, name, tag } = req.body;

      const data: Typings.Project.props[] = await CommonIns.Find({
        code,
      });

      if (data.length) {
        ResStatusIns.isExist(res);
        return;
      }

      await CommonIns.SaveOne({
        parentCode,
        code,
        name,
        tag,
        creatorId: creatorId || req.body.creatorId,
        createdTime: Date.now(),
        updatedTime: Date.now(),
      });

      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 更新
  async update(req: Request, res: Response) {
    try {
      const { id, name, tag } = <Partial<Typings.Project.props>>req.body;

      if (!id) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      const updateContent: Partial<Typings.Project.props> = {
        updatedTime: Date.now(),
      };

      if (name) {
        updateContent.name = name;
      }

      if (tag) {
        updateContent.tag = tag;
      }

      await CommonIns.UpdateOne({ id }, updateContent);
      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 查看
  async check(req: Request, res: Response) {
    try {
      const creatorId = CommonIns.getLoginUserId(
        req as Typings.Express.ConvertRequest
      );
      let { id } = req.query;

      if (!id && !creatorId) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      // 查询
      const projects: Typings.Project.props[] = await CommonIns.Find({
        id: id || creatorId,
      });

      if (!projects.length) {
        ResStatusIns.isNotExist(res);
        return;
      }

      ResStatusIns.Success(res, projects[0]);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 删除
  async remove(req: Request, res: Response) {
    try {
      let { id } = req.query;

      if (!id) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      // 删除用户数据
      await CommonIns.RemoveOne({ id });

      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }
}

export default Project;
